# Contributor: Leo <thinkabit.ukim@gmail.com>
# Contributor: Nulo <alpine@nulo.in>
# Contributor: Donoban <donoban@riseup.net>
# Contributor: psykose <alice@ayaya.dev>
# Maintainer: Nulo <alpine@nulo.in>
pkgname=telegram-desktop
# NOTE: Keep in mind that seemingly normal releases may be pre-releases.
# Check GitHub to make sure:
# https://github.com/telegramdesktop/tdesktop/releases
pkgver=4.0.2
pkgrel=1
pkgdesc="Telegram Desktop messaging app"
options="!check" # no test suite
url="https://desktop.telegram.org/"
# armhf blocked by qt5-qtwayland
# ppc64le and s390x blocked by tg_owt
# x86, riscv64: textrels
# riscv64: libdispatch
arch="all !x86 !ppc64le !s390x !armhf !riscv64"
license="GPL-3.0-or-later WITH OpenSSL"

depends="qt6-qtimageformats ttf-opensans"

[ "$CARCH" = "riscv64" ] && options="$options textrels"

# Sorted according to
# https://github.com/telegramdesktop/tdesktop/wiki/The-Packaged-Building-Mode
# libtgvoip is bundled as suggested by upstream
# jemalloc is disabled
# rlottie is bundled as suggested by upstream
# We are forcing it to use packaged libvpx with a patch below
makedepends="
	python3
	extra-cmake-modules
	qt6-qt5compat-dev
	qt6-qtsvg-dev
	qt6-qtwayland-dev

	tg_owt-dev
	opus-dev
	alsa-lib-dev
	pulseaudio-dev

	ffmpeg4-dev
	glibmm-dev
	libdispatch-dev
	gsl-dev
	hunspell-dev
	lz4-dev
	minizip-dev
	openal-soft-dev
	openssl1.1-compat-dev
	range-v3-dev
	rnnoise-dev
	tl-expected
	wayland-dev
	webkit2gtk-dev
	libxcb-dev
	xcb-util-keysyms-dev
	xxhash-dev
	zlib-dev

	libvpx-dev

	cmake
	samurai
	meson
	"

# small-sizes.patch is for postmarketOS. It makes UI elements fit in small
# screens.
# https://github.com/telegramdesktop/tdesktop/issues/8058#issuecomment-643461605

# telegram-desktop also assumes glibc, musl.patch fixes that.
source="
	https://github.com/telegramdesktop/tdesktop/releases/download/v$pkgver/tdesktop-$pkgver-full.tar.gz
	small-sizes.patch
	add-libvpx.patch
	musl.patch
	cstdint.patch
	"
builddir="$srcdir/tdesktop-$pkgver-full"

build() {
	cmake -B build -G Ninja . \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DDESKTOP_APP_DISABLE_CRASH_REPORTS=ON \
		-DTDESKTOP_API_ID=17349 \
		-DTDESKTOP_API_HASH=344583e45741c457fe1862106095a5eb \
		-DDESKTOP_APP_DISABLE_JEMALLOC=ON \
		-DDESKTOP_APP_USE_PACKAGED=ON \
		-DTDESKTOP_LAUNCHER_BASENAME=telegram-desktop
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

sha512sums="
0078b627d06dd8e655bd2bcc222ecbb3b15b7d88f858810bccdca84793ad02a60f4a4bf3808838752d65d4f6cffa3ddb5f70c5afc8e94cb6edae52101362feeb  tdesktop-4.0.2-full.tar.gz
3d480d712c94d720ff68bd88bc2edd007581c24cb43b76801af736fa4dff9610d154a7d2128634d18e910d058228d337f17aaf51fb10a2e93e0a9c52558ead1a  small-sizes.patch
c6b77a52547dc45f91f89cdd014c09fe318c49d34e88c4093f206b42ea3e61781a9447dbb4096424a845a519e101242bcac789272e8815a04c1be908cd304c37  add-libvpx.patch
e86df3cbf09f11e9fd49c73a78da1ea385b23dd90bbc271fb858d25035fd9c8ad14985ca64b39e87c09c556c14aef9c6ec83931515e32bbe020bcdbf389cc39d  musl.patch
114f3c44b0b9dfe9aa0d28617d83100859f781bca431515eed37dcaf52bb4cbc4c746d1c9c439999e65bd4fb360b72892b66da60325fe5d3c9c916d0337ff3b0  cstdint.patch
"
